#coding=utf-8
import urllib,time
from BeautifulSoup import BeautifulSoup

def get_s(item):
    while True:
        r = item.string
        if r: return r.strip()
        item = item.contents[0]
            
def get_tables(soup):
    t1 = 'SHOTS ON GOAL'
    t2 = 'Shots On Goal'
    sogs = soup.findAll(text=lambda x: (x.find(t1)!= -1) or (x.find(t2)!= -1))
    ts = [sog.parent.parent.parent for sog in sogs]
    
    return ts

def get_data_from_table(table):
    r = []
    for tr in table.contents[2:-1]:
        d = []
        for td in tr.contents:
            d.append(get_s(td))
        d[0] = d[0].replace('&nbsp;', ' ')
        r.append(d)
    return r
            
url = 'http://www.sportsline.com/nhl/gamecenter/boxscore/NHL_20061006_LA@ANA'
url2 = 'http://www.sportsline.com/nhl/gamecenter/boxscore/NHL_20081014_MIN@ATL'
#soup = BeautifulSoup(urllib.urlopen(url))

def get_data(url):
    r = []
    soup = BeautifulSoup(urllib.urlopen(url))
    ts = get_tables(soup)
    #print len(ts)
    for t in ts:
        r.append(get_data_from_table(t))
    return r
    
    
